from flask import render_template, redirect, url_for, request
from flask.helpers import flash
from flask_login import login_user, logout_user, current_user

from . import auth
from .forms import RegisterForm, LoginForm, ResetPasswordRequestForm, ResetPasswordForm
from app import db
from app.models import User
from app.tools import send_email


# 注册
@auth.route("/register", methods=["GET", "POST"])
def register():
    if current_user.is_authenticated:
        flash("已登录", "info")
        return redirect(url_for("main.index"))

    form = RegisterForm()
    if form.validate_on_submit():
        user = User(username=form.username.data,
                    password=form.password.data,
                    email=form.email.data)
        db.session.add(user)
        db.session.commit()
        send_email(
            user=user,
            subject="注册成功",
            template="mail/register.html",
        )
        flash("注册成功", "success")
        return redirect(url_for("auth.login"))
    return render_template("auth/register.html", form=form)

# 登录
@auth.route("/login", methods=["GET", "POST"])
def login():
    if current_user.is_authenticated:
        flash("已登录", "info")
        return redirect(url_for("main.index"))
        
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data, deleted="false").first()
        if user and user.verify_password(form.password.data):
            login_user(user, remember=form.remember.data)
            flash("登录成功", "success")
            return redirect(url_for("main.index"))
        flash("邮箱不存在或者密码错误！", "danger")
    return render_template("auth/login.html", form=form)

# 退出登录
@auth.route("/logout")
def logout():
    if current_user.is_authenticated:
        logout_user()
        flash("已成功退出登录", "info")
    return redirect(url_for("main.index"))

# 找回密码
@auth.route("/reset_password_request", methods=["GET", "POST"])
def reset_password_request():
    form = ResetPasswordRequestForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data, deleted="false").first()
        if user:
            token = user.generate_token()
            send_email(
                user=user,
                subject="找回密码",
                template="mail/reset_password.html",
                token=token
            )
            flash("找回密码的邮件已经发送到你邮箱", "info")
            return redirect(url_for("auth.login"))
        flash("邮箱错误！", "danger")
    return render_template("auth/reset_password_request.html", form=form)

# 密码重置
@auth.route("/reset_password", methods=["GET", "POST"])
def reset_password():
    form = ResetPasswordForm()
    if form.validate_on_submit():
        user = User.check_token(request.args.get("token"))
        if user:
            user.password = form.password.data
            db.session.commit()
            flash("密码重置成功", "success")
            return redirect(url_for("auth.login"))
        flash("哪里出错了呐~", "danger")

    return render_template("auth/reset_password.html", form=form)

